package co.id.btel.create.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import utilities.CellModif;
import utilities.ChunkModif;
import utilities.Constants;
import utilities.PdfUtil;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfTotal {

	private static CellModif cwht;
	private static PdfPTableEvent tableBackground;
	private static PdfPTable table;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String FILE = "E:/BTE/samplePdf/Total.pdf";
		try {
			Document document = new Document();
			File file = new File(FILE);
			if (file.exists())
				file.delete();
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream(FILE));
			ChunkModif event = new ChunkModif();
			writer.setPageEvent(event);
			document.open();
			PdfUtil.addMetaData(document);
			PdfTotal pt = new PdfTotal();
			pt.addTotal(document);
			document.close();
			System.out.print("done!" + FILE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PdfTotal() throws DocumentException, IOException {
		super();
		table = new PdfPTable(1);
		table.setWidthPercentage(100);
		tableBackground = new TableBackground();
		cwht = new CellModif("roundRect", 4);

	}

	public void addTotal(Document document) throws DocumentException {

		Paragraph p = new Paragraph();
		PdfPCell c1 = new PdfPCell(p);
		c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		c1.setBorderWidth(0);
		c1.addElement(addTotalInfo(document));
		table.addCell(c1);
		table.setTableEvent(tableBackground);
		document.add(table);

	}

	private static PdfPTable addTotalInfo(Document document)
			throws DocumentException {

		PdfPTable table = new PdfPTable(3);
		table.setWidthPercentage(100);
		table.setWidths(new int[] { 19, 1, 9 });

		// row 0
		Paragraph p = new Paragraph("");
		PdfPCell c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		table.addCell(c1);

		// row 0.1
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		table.addCell(c1);

		// row 0.2
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		table.addCell(c1);

		// row 1
		p = new Paragraph("");
		Chunk ch = new Chunk("Jumlah yang harus dibayarkan |",
				FontFactory.getFont(FontFactory.HELVETICA, Constants.FONT_E,
						Font.NORMAL, BaseColor.WHITE));
		float chWidth = ch.getWidthPoint();
		p.add(ch);
		ch = new Chunk("Total amount to be paid", FontFactory.getFont(
				FontFactory.HELVETICA, Constants.FONT_E, Font.ITALIC,
				BaseColor.WHITE));

		p.add(ch);
		chWidth = chWidth + ch.getWidthPoint();
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		CellModif event = new CellModif("line", chWidth,0);
		c1.setCellEvent(event);
		table.addCell(c1);

		// row 1.1
		p = new Paragraph("");
		ch = new Chunk("Rp.", FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.WHITE));
		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		table.addCell(c1);

		// row 1.2
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		c1.setBorderWidth(0);
		c1.addElement(PdfUtil.addCharge("100"));
		c1.setCellEvent(cwht);
		table.addCell(c1);

		// row 2
		p = new Paragraph("");
		ch = new Chunk("Terbilang", FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.WHITE));

		p.add(ch);
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setColspan(3);
		table.addCell(c1);

		// row 3
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		c1.setBorderWidth(0);
		c1.addElement(PdfUtil.addStatement("Seratus Rupiah"));
		c1.setCellEvent(cwht);
		c1.setColspan(3);
		table.addCell(c1);

		// row 4
		p = new Paragraph("");
		c1 = new PdfPCell(p);
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		c1.setBorderWidth(0);
		c1.addElement(PdfUtil.addStatement("Saja"));
		c1.setCellEvent(cwht);
		c1.setColspan(3);
		table.addCell(c1);

		return table;

	}

	class TableBackground implements PdfPTableEvent {

		public void tableLayout(PdfPTable table, float[][] width,
				float[] height, int headerRows, int rowStart,
				PdfContentByte[] canvas) {
			PdfContentByte background = canvas[PdfPTable.BASECANVAS];
			background.saveState();
			// background.setCMYKColorFill(0xFF, 0x00, 0xFF, 0x00);
			background.setColorFill(Constants.AHA_BASE);
			background.roundRectangle(width[0][0],
					height[height.length - 1] - 2, width[0][1] - width[0][0],
					height[0] - height[height.length - 1], 4);
			background.fill();
			background.restoreState();
		}

	}
}
